package ${package};

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.ahccc.common.util.StrUtil;
import com.ahccc.common.util.bootStrapTable.PageVO;
import com.ahccc.common.ExecuteState;
import com.ahccc.common.ResponseData;
import com.ahccc.common.ResultMsg;
import com.ahccc.common.BaseController;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import ${modelpackage}.${name};
import ${servicepackage}.${name}Manager;
/**
* @copyright:${copyright}
* @Author:${author}
* @Date:${datetime}
* @Description:
*/
@Controller
@RequestMapping("${sname}")
public class ${name}Controller extends BaseController{

	@Resource
	private ${name}Manager manager;

    @GetMapping("/${sname}")
    /**@RequiresPermissions("${sname}:${sname}")**/
    String ${sname}(){
    	return "/app/${sname}/${sname}";
    }

    @GetMapping("/add")
	/**@RequiresPermissions("${sname}:add")**/
    String add(){
    	return "/app/${sname}/add";
    }

    @GetMapping("/edit/{${pk.simpleName}}")
	/**@RequiresPermissions("${sname}:edit")**/
    String edit(@PathVariable("${pk.simpleName}") ${pk.javaType} ${pk.simpleName},Model model){
		${name} ${sname} = manager.get(${pk.simpleName});
   		model.addAttribute("${sname}", ${sname});
		return "/app/${sname}/edit";
    }

    @GetMapping("/detail/{${pk.simpleName}}")
	/**@RequiresPermissions("${sname}:detail")**/
    String detail(@PathVariable("${pk.simpleName}") ${pk.javaType} ${pk.simpleName},Model model){
        ${name} ${sname} = manager.get(${pk.simpleName});
        model.addAttribute("${sname}", ${sname});
        return "/app/${sname}/detail";
    }

	/**
	 * 按指定id的查询菜单信息
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/get/{id}")
	@ResponseBody
	public ${name} get(@PathVariable() <#list fields as f>${f.javaType}</#list> id) {
		return manager.get(id);
	}

	/**
	 * 修改或新增
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/save", method = { RequestMethod.PUT, RequestMethod.POST })
	/**@RequiresPermissions("${sname}:save")**/
	@ResponseBody
	public ResponseData save(HttpServletRequest request) {
		${pk.javaType} ${pk.simpleName}=null;
		${name} vo = new ${name}();
		String parameter${pk.fixCode}=request.getParameter("${pk.simpleName}");
		if (!StringUtils.isEmpty(parameter${pk.fixCode})) {
			${pk.simpleName} = ${pk.javaType}.valueOf(parameter${pk.fixCode});
		}
		try{
			if (!StringUtils.isEmpty(${pk.simpleName})) {
				vo = manager.get(${pk.simpleName});
			}
			bind(request, vo);
			manager.save(vo);
			return new ResponseData(ExecuteState.SUCCESS, ResultMsg.saveSucceed, vo);
		} catch (Exception e) {
			return new ResponseData(ExecuteState.ERROR, e.getMessage(), vo);
		}
	}


     /**
	 * 删除记录 单条
	 */
	@DeleteMapping(value = "/delete/{id}")
	/**@RequiresPermissions("${sname}:delete")**/
	@ResponseBody
	public ResponseData delete(@PathVariable() <#list fields as f>${f.javaType}</#list> id) {
		try {
			manager.remove(id);
			return new ResponseData(ExecuteState.SUCCESS, ResultMsg.delSucceed, id);
		} catch (Exception e) {
			return new ResponseData(ExecuteState.ERROR, e.getMessage(), id);
		}
	}
    /**
    * 删除记录 多条
    */
	@PostMapping(value = "/batchRemove")
	/**@RequiresPermissions("${sname}:delete")**/
	@ResponseBody
	public ResponseData batchRemove(@RequestParam("ids[]")  <#list fields as f>${f.javaType}</#list>[] ids) {
		try {
			manager.remove(ids);
			return new ResponseData(ExecuteState.SUCCESS, ResultMsg.delSucceed, ids);
		} catch (Exception e) {
			return new ResponseData(ExecuteState.ERROR, e.getMessage(), ids);
		}
	}

	/** 默认动态查询 */
	@GetMapping(value = "/list")
	@ResponseBody
	public PageVO list(HttpServletRequest request,Integer pageNumber,Integer pageSize,String sortName,String sortOrder) {
		${name} vo = new ${name}();
		bind(request, vo);
		Sort sort=new Sort(Sort.Direction.ASC,"id");
		if(!StrUtil.isEmpty(sortName)&&!StrUtil.isEmpty(sortOrder)){
			sort=new Sort("asc".equals(sortOrder)?Sort.Direction.ASC:Sort.Direction.DESC,sortName);
		}
		return manager.findAll(vo,  sort,  pageNumber,  pageSize);
	}
}
